import { AppDispatch } from '@/store';
import {
  EditProfileApi,
  GetProfileApi,
  GetUserInfoApi,
  UploadPhotoApi,
} from '@/api/user';
import { ProfileType } from '../reducer/user';

/** 获取用户个人资料Action */
export function GetProfileAction() {
  return async (dispatch: AppDispatch) => {
    let { data } = await GetProfileApi();
    dispatch({ type: 'user/SaveProfile', payload: data });
  };
}

/** 获取用户自己的信息Action */
export function GetUserInfoAction() {
  return async (dispatch: AppDispatch) => {
    let { data } = await GetUserInfoApi();
    dispatch({ type: 'user/SaveUserInfo', payload: data });
  };
}

/** 更新Profile /Photo/ 的Action */
export function UpdateProfileAction(formdata: FormData) {
  return async (dispatch: AppDispatch) => {
    let { data } = await UploadPhotoApi(formdata);
    console.log('data  ----->  ', data);
    dispatch({ type: 'user/UpdateProfile', payload: data });
  };
}

/** 更新Profile其他资料 的Action */
export function UpdateUserProfileAction(ProfilesData: ProfileType) {
  return async (dispatch: AppDispatch) => {
    await EditProfileApi(ProfilesData);
    await dispatch({ type: 'user/SaveProfile', payload: ProfilesData });
  };
}
